package com.dana1.nos.util;

import java.util.Collections;
import java.util.Iterator;

/** 
 * This iterator takes an iterator of iterators, and returns the elements of each of those iterators, in sequence.
 * 
 * @author Dana
 *
 * @param <E>
 */
public class IteratorOfIterators<E> implements Iterator<E> {

	private Iterator<Iterator<E>> source;
	private Iterator<E> currentIterator;
	private Iterator<E> lastIterator;
	
	public IteratorOfIterators(Iterator<Iterator<E>> source) {
		super();
		this.source = source;
		currentIterator = Collections.emptyIterator();
	}

	@Override
	public boolean hasNext() {
		while ((!currentIterator.hasNext())&&(source.hasNext()))
			currentIterator = source.next();
		return currentIterator.hasNext();
	}

	@Override
	public E next() {
		hasNext();
		lastIterator = currentIterator;
		return currentIterator.next();
	}

	@Override
	public void remove() {
		lastIterator.remove();
	}

}
